package com.cn.utlis;

import java.io.File;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenyuncong
 * @date 2020-05-15 1:29
 */
public class FileListUtils {

    public static void main(String[] args) {
//        File f1 = new File("D:\\data");
        //深度搜索
//        getPathDFS(f1);

        System.out.println("======================");

        //广度搜索
//        getPathBFS(f1);

        checkFile("", "");
    }

    public static List<String> checkFile(String FILESERVERURL, String FILEPATH) {
        File directory = new File("D:\\ihkvr");
        ArrayList<String> dirctorys = new ArrayList<String>();
//        File directory = new File(folderPath);
        if (!directory.isDirectory()) {
//            throw new ScanFilesException('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^");
        }
        if (directory.isDirectory()) {
            File[] filelist = directory.listFiles();
            File toFile = null;
            for (int i = 0; i < filelist.length; i++) {
                /**如果当前是文件夹，进入递归扫描文件夹**/
                if (filelist[i].isDirectory()) {
                    toFile = new File(filelist[i].getAbsolutePath());
                    System.out.println();
                    System.out.println(filelist[i].getAbsolutePath());
                    File[] tofilelist = toFile.listFiles();
                    for (int n = 0; n < tofilelist.length; n++) {
//                        System.out.println(tofilelist[n].getAbsolutePath());
                        if (tofilelist[n].isDirectory()) {
                            String path = tofilelist[n].getAbsolutePath();

                        } else {
                            // tofilelist[n].getAbsolutePath() //拿到了图片
                        }

                    }
                }
                /**非文件夹**/
                else { // filelist[i].getAbsolutePath()
//                    scanFiles.add();
                }
            }
        }
        return null;
    }

    private static void getPathBFS(File f1) {
        //输出第一层目录
        System.out.println(f1);
        //运用队列进行广度搜索
        ArrayDeque<File> deque = new ArrayDeque<File>();
        //第一个File实例入队
        deque.offer(f1);
        //队列为空时停止
        while (!deque.isEmpty()) {
            //取队头
            File[] ff = deque.peek().listFiles();
            //出队
            deque.poll();
            for (int i = 0; i < ff.length; i++) {
                //如果是文件夹入队
                if (ff[i].isDirectory()) {
                    System.out.println(ff[i]);
                    deque.addLast(ff[i]);
                } else {
                    System.out.println(ff[i]);
                }
            }
        }
    }

    private static void getPathDFS(File f1) {
        System.out.println(f1.getName());
        File[] file = f1.listFiles();
        for (int i = 0; i < file.length; i++) {
            //如果是个文件夹就继续搜索
            if (file[i].isDirectory()) {
                getPathDFS(file[i]);
            } else {
                //不是文件夹就直接输出
                System.out.println(file[i]);
            }
        }
    }
}
